package com.example.contactos;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {

	public static final String AGENDA = "com.example.contactos.Agenda";
	public static final int ADD_CONTACT = 1;
	public static final int EDIT_CONTACT = 2;
	protected static final String NOME_CONTACTO = "nome_contacto";
	protected static final String BI_CONTACTO = "bi_contacto";

	private Agenda agenda;
	private ListView lstContactos;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		agenda = Agenda.getInstance();

		atualizarContactos();
		//textView_contactos = (TextView) findViewById(R.id.textView_contactos);		

		//refreshTextViewContactos();

		lstContactos = (ListView) findViewById(R.id.contactList);
		lstContactos.setLongClickable(true);

		lstContactos.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				final Pessoa pessoaSelecionada = (Pessoa) arg0.getItemAtPosition(arg2);
				
				AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
				alertBuilder.setTitle(R.string.eliminar);
				alertBuilder.setMessage(R.string.eliminar_o_contacto_);
				alertBuilder.setPositiveButton(R.string.confirmar, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						Agenda.getInstance().remover(pessoaSelecionada);
						atualizarContactos();
					}
				})
				.setNegativeButton(R.string.cancelar, null);

				AlertDialog dialogo = alertBuilder.create();
				dialogo.show();

				return true;
			}});
		
		lstContactos.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				Pessoa pessoaSelecionada = (Pessoa) arg0.getItemAtPosition(arg2);
				Intent i = new Intent(MainActivity.this, DetalhesActivity.class);	
				i.putExtra(NOME_CONTACTO, pessoaSelecionada.getNome());
				i.putExtra(BI_CONTACTO, pessoaSelecionada.getBi());
				startActivityForResult(i, EDIT_CONTACT);
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);

		MenuItem new_contact = menu.findItem(R.id.new_contact);			
		new_contact.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent i = new Intent(MainActivity.this, DetalhesActivity.class);				
				startActivityForResult(i, ADD_CONTACT);

				return false;
			}
		});		

		MenuItem search_contact = menu.findItem(R.id.search_contact);		
		search_contact.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent i = new Intent(MainActivity.this, PesquisaActivity.class);				
				i.putExtra(AGENDA, agenda);				
				startActivity(i);

				return false;
			}
		});	

		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == ADD_CONTACT) {
			if (resultCode == RESULT_OK) {
				//agenda.adicionar((Pessoa) data.getSerializableExtra(DetalhesActivity.CONTACTO));
				atualizarContactos();
				//refreshTextViewContactos();
				Toast.makeText(this, "Contacto adicionado.", Toast.LENGTH_LONG).show();
			}
		}
		
		if (requestCode == EDIT_CONTACT) {
			if (resultCode == RESULT_OK) {
				atualizarContactos();
				Toast.makeText(this, "Contacto modificado", Toast.LENGTH_LONG).show();
			}
		}
	}

	void atualizarContactos(){
		ArrayAdapter<Pessoa> adaptador = new ArrayAdapter<Pessoa>(this,
				android.R.layout.simple_list_item_1, agenda.getAll());

		ListView lista = (ListView) findViewById(R.id.contactList); 

		lista.setAdapter(adaptador);
	}
}
